using System;
using System.Web;
using System.Web.UI.WebControls;

namespace SystemFrameworks.Util
{
	/// <summary>
	/// Summary description for ScriptHelper.
	/// </summary>
	public sealed class ScriptHelper
	{
		private ScriptHelper()
		{
		}

		private const string SCRIPT			= "<script>{0}</script>";
		private const string CLOSE			= "window.close();";
		private const string ALERT			= "alert('{0}');";
		private const string CONFIRM		= "return confirm('{0}');";
		private const string SUBMIT_FALSE	= "return false;";
		private const string SUBMIT_TRUE	= "javascript:document.forms( 0 ).onsubmit='return true;';";
		private const string ON_CLICK		= "OnClick";
		private const string RENEW			= "window.opener.document.all('{0}').click();";


		#region Public Method 

		
		#region Write to Response

		/// <summary>
		/// Close Form
		/// </summary>
		public static void CloseForm()
		{
			BoxWrite( CLOSE ); 
		}

		/// <summary>
		/// Alert message
		/// </summary>
		/// <param name="message"></param>
		public static void Alert( string message )
		{
			BoxWrite( string.Format( ALERT, CleanMessage( message ) ) );
		}

		/// <summary>
		/// Cause OnClick event of Button in Opener Form to renew partly
		/// </summary>
		/// <param name="buttonId"></param>
		public static void RenewOpener( string buttonId )
		{
			string renew = string.Format( RENEW, buttonId );
			BoxWrite( renew + CLOSE );
		}

		#endregion


		#region Add to Attributes

		/// <summary>
		/// Add confirm message for WebControl's OnClick event
		/// </summary>
		/// <param name="control"></param>
		/// <param name="confirm"></param>
		public static void ClickConfirm( WebControl control, string confirm )
		{
			control.Attributes.Add( ON_CLICK, string.Format( CONFIRM, CleanMessage( confirm ) ) );
		}

		/// <summary>
		/// Add alert message for WebControl's OnClick event, and do not cause PostBack
		/// </summary>
		/// <param name="control"></param>
		/// <param name="message"></param>
		public static void ClickFalse( WebControl control, string message )
		{
			string alert = string.Format( ALERT, CleanMessage( message ) );
			control.Attributes.Add( ON_CLICK, alert + SUBMIT_FALSE );
		}

		/// <summary>
		/// Make WebControl's OnClick event always cause PostBack, ignoring validation
		/// </summary>
		/// <param name="control"></param>
		/// <param name="message"></param>
		public static void ClickTrue( WebControl control )
		{
			control.Attributes.Add( ON_CLICK, SUBMIT_TRUE );
		}

		#endregion


		#endregion

		#region Private Property

		/// <summary>
		/// Current Response
		/// </summary>
		private static HttpResponse Response
		{
			get
			{
				return HttpContext.Current.Response;
			}
		}

		#endregion

		#region Private Method

		/// <summary>
		/// Clean durty char in message to Alert
		/// </summary>
		/// <param name="script"></param>
		/// <returns></returns>
		private static string CleanMessage( string message )
		{
			return message.Replace(  "'", " " );
		}

		/// <summary>
		/// Add head and tail to script and write to response
		/// </summary>
		/// <param name="script"></param>
		/// <returns></returns>
		private static void BoxWrite( string script )
		{
			Response.Write( string.Format( SCRIPT, script ) );
		}

		#endregion
	}
}
